home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
online
/
source
/
cpp
/
graphics
/
Conics.sit.hqx
/
Conics
/
Class Libraries
/
Cleanup
/
Cleanup.cpp
next >
Wrap
Text File
|
1996-11-08
|
6KB
|
174 lines
//Copyright 1996 Aidan Cully
//All rights reserved
#include <Dialogs.h>
#include "Cleanup.h"
TCleanup mainClean;
void DrawAlert( unsigned char *theText ) {
::ParamText( theText, 0l, 0l, 0l );
Alert( 128, 0l );
}
// TCleanup::RegisterDirtyAction()
// uses:
// Use this procedure to place a procedure in the procedure list which will be called
// after two conditions are met:
// 1: that an action is pending in the "Dirty" list (normally this occurs through
// calls to "NotifyDirty", which must be called after "RegisterDirtyAction"
// 2: that Clean be called. This can be called from anywhere, but will most likely
// be called (depending on the variable you use for cleanup) every time through the
// the event loop.
// in:
// UInt32 procID: a unique ID number which is forever bound to the second parameter
// CleanupProc theProc: a procedure which takes a void* and a procID as parameters, and
// performs some action or other based on these datas.
// return value:
// UInt8: an error code. Current possibilities are:
// kSuccess == No Error
// kDuplicateActionErr == the procID variable is already in use
// kCleanupMemErr == out of memory
UInt8 TCleanup::RegisterDirtyAction( UInt32 procID, CleanupProc theProc ) {
ProcType *theEl;
//if nothing is in the procedure list as yet, then insert the first element.
if( !procs.MoveFirst() ) {
//create and initialize the element structure. We need to have an element structure
//because of the *extremely* rudimentary way in which TList operates.
theEl = new ProcType;
if( !theEl )
return( kCleanupMemErr );
theEl->procID = procID;
theEl->theProc = theProc;
if( !procs.AddNext() )
return( kCleanupMemErr );
procs.SetData( (UInt32)theEl );
return( kSuccess );
} else { //otherwise, we have to check that the procID variable is actually unique before
//inserting.
//the if block above called upon entry "procs.MoveFirst()," so we don't have to bother
//here.
UInt32 temp;
do { //loop through every element in the list to find possibly duplicate procID's.
procs.GetData( temp );
theEl = (ProcType*)temp;
if( theEl->procID == procID ) //Found one?
return( kDuplicateActionErr ); //then return the error code.
} while( procs.MoveNext() );
//create the new variables (in the TList, and in the ProcType*) checking for memory
//allocation errors.
if( !procs.AddNext() )
return( kCleanupMemErr );
//now, everything's fine.
theEl = new ProcType;
if( !theEl )
return( kCleanupMemErr );
theEl->procID = procID;
theEl->theProc = theProc;
procs.SetData( (UInt32)theEl );
return( kSuccess );
}
}
// TCleanup::NotifyDirty()
// uses:
// Use NotifyDirty to have a function called with certain data the next time Clean() is
// called for this particular instance of TCleanup.
// in:
// UInt32 procID: unique ID number identifying which procedure to call
// void *procData: data to be used locally by said procedure
// return value:
// UInt8: possible return values are at present:
// kSuccess == everything's groovin.
// kActionNotFoundErr == the procID specified is invalid.
// kCleanupMemErr == out of memory.
UInt8 TCleanup::NotifyDirty( UInt32 procID, void *procData ) {
DataType *theEl;
ProcType *procEl;
Boolean found = false;
//Verify that a procedure IS identified by procID.
if( !procs.MoveFirst() )
return( kActionNotFoundErr );
UInt32 temp;
do {
procs.GetData( temp );
procEl = (ProcType*)temp;
if( procEl->procID == procID )
found = true;
} while( !found && procs.MoveNext() );
if( !found )
return( kActionNotFoundErr );
//The procedure does exist, now add an element to the stack signifying that this procedure
//must be called with this particular data.
theEl = new DataType;
if( !theEl )
return( kCleanupMemErr );
theEl->procID = procID;
theEl->procData = procData;
if( !queue.Enqueue( (UInt32)theEl ) )
return( kCleanupMemErr );
//Wow, everything went fine!
return( kSuccess );
}
// TCleanup::Clean()
// uses:
// Use this procedure to call all other procedures that the TCleanup object has been told
// to call (usually by use of the NotifyDirty() procedure).
// in: none.
// return value:
// UInt8: the return value can be (at present) one of the following:
// kSuccess == Far Out in a Happening Kind of Way (™)
// kActionNotFound == one of the elements in the list had no appropriate action.
// kActionLocalErr == the action procedure called had some kind of problem with it.
UInt8 TCleanup::Clean() {
DataType *theEl;
ProcType *theProc;
UInt32 temp;
Boolean found;
while( queue.Dequeue( temp ) ) {
theEl = (DataType*) temp;
if( !procs.MoveFirst() )
return( kActionNotFoundErr );
found = false;
do {
procs.GetData( temp );
theProc = (ProcType*)temp;
if( theProc->procID == theEl->procID )
found = true;
else if( !procs.MoveNext() ) {
delete theEl;
return( kActionNotFoundErr );
}
} while( !found );
if( !theProc->theProc( theEl->procID, theEl->procData ) ) {
delete theEl;
return( kActionLocalErr );
}
delete theEl;
}
return( kSuccess );
}
// TCleanup::~TCleanup()
// Destroys all variables associated with TCleanup. DOES NOT call the action procedures (at
// this point, that could even be dangerous).
TCleanup::~TCleanup() {
ProcType *theProc;
DataType *theData;
UInt32 dataInt;
if( procs.MoveFirst() )
do {
procs.GetData( dataInt );
theProc = (ProcType*)dataInt;
delete theProc;
} while( procs.Remove() );
while( queue.Dequeue( dataInt ) ) {
theData = (DataType*)dataInt;
delete theData;
}
}